package com.gitee.wsl.transform.converter.case

import com.gitee.wsl.transform.converter.StringConverter
import com.gitee.wsl.transform.split.WordSplitter


/**
 * An interface that defines a case formatter that can be used to join a collection of words into single string.
 */
interface CaseFormatter:StringConverter {
    /**
     * Joins all elements of [words], appending the result to [appendable].
     */
    fun formatTo(appendable: Appendable, words: Iterable<String>)

    override fun convert(a: String): String = format(a)
}

/**
 * Joins all elements of [words], appending the result to [appendable].
 */
fun CaseFormatter.formatTo(appendable: Appendable, vararg words: String) = formatTo(appendable, words.asIterable())

/**
 * Joins all elements of [words] and returns the resulting string.
 */
fun CaseFormatter.format(words: Iterable<String>): String = buildString { formatTo(this, words) }

/**
 * Joins all elements of [words] and returns the resulting string.
 */
fun CaseFormatter.format(vararg words: String) = format(words.asIterable())

/**
 * Returns a copy of [string] converted to another case by splitting it into multiple words using [wordSplitter] and joining them using [CaseFormatter].
 */
fun CaseFormatter.format(string: String, wordSplitter: WordSplitter = universalWordSplitter()) = format(wordSplitter.splitToWords(string))